proc client:presence {from type xlist args} {
global bot admins presence pfrom name
set a "presence"
	set jid [user_from_jid "$from"]
	set res [resource_from_jid "$from"]
	::debug $a "jid==$jid" "res==$res"
	set users userlist
	set founduser 0
   
	if {$jid==$bot(admin)} {
		set founduser 1
	} elseif {[lsearch -exact $admins $jid]!=-1} {
		set founduser 1
	}
   
	# Another user is requesting to subscribe to my presence (put me on his rostr)

	switch -- $type {
		error {::debug $a "bad input presence" "may be wrong output presence"}
		unavailable {
			::debug $a unavailable $from
			#unset presence(type,$from)
			set presence(status,$from) 0
			#unset presence(priority,$from)
			#catch { unset presence(meta,$from) }
			#catch { unset presence(icon,$from) }
			#catch { unset presence(show,$from) }
			#catch { unset presence(loc,$from) }
			#catch { unset presence(x,$from) }
			#catch { unset presence(error,$from) }

			#putlog "presence: $from=>unavailable"
			eval {hook::run unavailable $from} $args
		}
		available {
			::debug $a available $from
			foreach {attr val} $args {
				switch -- $attr {
					-status   {set presence(status,$from)   $val}
					-priority {set presence(priority,$from) $val}
				}
				set mucinfo(af,$from) "none"
				set mucinfo(role,$from) "none"
				set mucinfo(jid,$from) $from
				set name($jid) "$jid"
				::debug $a "name($jid) set as" "$jid"
				set name($from) "$jid"
				::debug $a "name($from) set as" "$jid"
			}
			eval {hook::run available $from} $args
		}
		subscribe {
			if {$founduser} {
				if {$founduser == 1} {
					putlog "INFO: $jid добавлен в ростер"
					jlib::send_presence -to [list $jid] -type "subscribed"
					jlib::send_msg $from -body "Поздравляю.вы авторизованы и это значит что вы имеете полный доступ ко всем командам.ну почти ко всем :-)"
				} else {
					putlog "INFO: запрос авторизации от $jid ($from): $stat"
					jlib::send_msg $from -body "Извините,но мне разрешено добавлять в ростер только владельца или администраторов"
					jlib::send_presence -to $from -type "unsubscribed"
				}
			}
			eval {hook::run subscribe $from} $args
			return
		}
   
		subscribed {putlog "REC: Сообщение авторизации";eval {hook::run subscribed $from} $args;return}
		unsubscribe {putlog "REC: Сообщение Отписьки )))";eval {hook::run unsubscribe $from} $args;return}
		unsubscribed {putlog "REC: Авторизация удалена";eval {hook::run unsubscribed $from} $args;return}
		probe {}
   
		default {
			set type available
			::debug $a type default_available
			set presence(type,$from)     available
			set presence(status,$from)   ""
			set presence(priority,$from) 0
			set presence(meta,$from)     ""
			set presence(icon,$from)     ""
			set presence(show,$from)     available
			set presence(loc,$from)      ""
			set presence(x,$from)        $xlist

			if {$xlist!=""} {
				::debug $a xlist<> 0
				#if {$name($jid)==""} {
				#	putlog $name($jid)
				#	set name($jid) $from
					::debug $a "set name($jid)" "$from"
				#}
				if {[info exists name($jid)]==0} {
					set name($jid) $from
				}
				::debug $a "set name($from)" "$from"
				foreach {attr val} $args {
					switch -- $attr {
						-status   {set presence(status,$from)   $val}
						-priority {set presence(priority,$from) $val}
						-meta     {set presence(meta,$from)     $val}
						-icon     {set presence(icon,$from)     $val}
						-show     {set presence(show,$from)     $val}
						-loc      {set presence(loc,$from)      $val}
					}
				}
				if {[info exists xlist]==1} {
					set pfrom $from
					parse_x $xlist
				}
				eval {hook::run availablex $from} $args;
			} else {
				set mucinfo(af,$from) "none"
				set mucinfo(role,$from) "none"
				set mucinfo(jid,$from) "$from"
				set name($jid) $from
				::debug $a "set name($jid)" "$jid "
				set name($from) $from
				::debug $a "set name($from)" "$jid"
				eval {hook::run availablew $from} $args;
			}
		eval {hook::run client_presence $from $type $xlist} $args
		}
	}
}

proc parse_x {x} {
   foreach item $x {
      parse_xx $item
   }
}


proc parse_xx {child} {

    wrapper:splitxml $child tag vars isempty chdata children
    switch -exact -- $tag { \
   x {parse_x_item $children}
   default {putlog "parse_xx: Unknown tag $tag"}
    }
}

proc parse_x_item {child} {

   wrapper:splitxml $child tag vars isempty chdata children
   wrapper:splitxml $tag tag vars isempty chdata children


   switch -exact -- $tag { \
      item {parse_item $vars}
   default {putlog "parse_x_item: Unknown tag $tag"}
    }
}

proc parse_item {child} {
global pfrom mucinfo

wrapper:splitxml $child tag vars isempty chdata children

	if {$tag=="affiliation"} {
		set mucinfo(af,$pfrom) $vars;putlog "affiliation is $vars"
		set mucinfo(role,$pfrom) $chdata;putlog "role is $chdata"
		return
	} elseif {$tag=="jid"} {
		set mucinfo(jid,$pfrom) $vars
		set mucinfo(af,$pfrom) $chdata;putlog "affiliation is $chdata"
		wrapper:splitxml $children tag vars isempty chdata chil
		set mucinfo(role,$pfrom) $vars;putlog "role is $vars";putlog "jid is $mucinfo(jid,$pfrom)"
		return
	}
	set mucinfo(jid,$pfrom) $pfrom
	eval {hook::run muc_available $from} $args
}
